package com.javer.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.javer.domain.Article;
import com.javer.domain.Comment;
import com.javer.domain.PageView;
import com.javer.service.ArticleService;
import com.javer.util.Constants;
import com.javer.util.IpAddressUtil;
import com.javer.util.WebUtil;

@Controller("/article")
public class ArticleAction extends DispatchAction {
	private final Log logger = LogFactory.getLog(getClass());
	private static final int PAGE_SIZE = 30;
	@Autowired private ArticleService articleService;

	public ActionForward getInterestTopicArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int atype = Integer.valueOf(req.getParameter("atype"));
		req.setAttribute("articles", articleService.getNewArticle(atype, 3));
		return new ActionForward("/pages/article/interestlist.jsp");
	}
	
	public ActionForward getOneArticleByType(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int atype = Integer.valueOf(req.getParameter("atype"));
		List<Article> articles = articleService.getNewArticle(atype, 1);
		req.setAttribute("article", !articles.isEmpty() ? articles.get(0) : null);
		return new ActionForward("/pages/article/shownewest.jsp");
	}
	
	// 前台分页显示
	public ActionForward getArticleByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Article> pv = WebUtil.createPageView(req);
		pv.setPageSize(PAGE_SIZE);
		String type = req.getParameter("atype");
		req.setAttribute("pv", articleService.getPageByType(pv, type));
		return new ActionForward("/pages/article/articlepagelist.jsp");
	}
	
	public ActionForward addArticleComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String authCode = String.valueOf(req.getParameter("authCode"));
		String authCodeFromSession = (String) req.getSession().getAttribute(Constants.AuthCode);
		req.getSession().removeAttribute(Constants.AuthCode);
		
		Comment comment = new Comment();
		BeanUtils.populate(comment, req.getParameterMap());
		
		WebUtil.handleCommentContent(comment);
		
		comment.setUaddress(IpAddressUtil.getIpAddressByIp(comment.getUip()));
		
		if (!authCode.equalsIgnoreCase(authCodeFromSession)) {
			req.setAttribute("error", "验证码错误！");
			return new ActionForward("/article.do?method=showArticle&id=" + comment.getTid());
		}
		
		articleService.addArticleComment(comment);
		return new ActionForward("/article.do?method=showArticle&id=" + comment.getTid(), true);
	}
	
	public ActionForward findByType(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Article> pv = WebUtil.createPageView(req);
		String type = req.getParameter("atype");
		req.setAttribute("pv", articleService.getPageByType(pv, type));
		return mapping.findForward("list-article");
	}
	
	public ActionForward showArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		req.setAttribute("article", articleService.getById(id));
		req.setAttribute("comments", articleService.getHotCommentsByArticle(id));
		return new ActionForward("/pages/article/show.jsp");
	}
	
	public ActionForward getNewArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int limit = 18;
		if (req.getParameter("limit") != null) {
			limit = Integer.valueOf(req.getParameter("limit"));
		}
		int atype = Integer.valueOf(req.getParameter("atype"));
		req.setAttribute("articles", articleService.getNewArticle(atype, limit));
		return new ActionForward("/pages/article/list.jsp");
	}
	
	public ActionForward modifyArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Article article = new Article();
		BeanUtils.populate(article, req.getParameterMap());
		try {
			articleService.updateArticle(article);
		} 
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			req.setAttribute("error", e.getMessage());
			return new ActionForward("/manage/article/form.jsp");
		}
		return new ActionForward("article.do?method=listArticleByPage", true);
	}
	
	public ActionForward feedback(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Article article = new Article();
		BeanUtils.populate(article, req.getParameterMap());
		article.setTitle("信息反馈");
		try {
			articleService.addArticle(article);
		} 
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			req.setAttribute("error", e.getMessage());
		}
		return new ActionForward("/pages/feedback.jsp?temp=1");
	}
	
	public ActionForward addArticle(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Article article = new Article();
		BeanUtils.populate(article, req.getParameterMap());
		try {
			articleService.addArticle(article);
		} 
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			req.setAttribute("error", e.getMessage());
			return new ActionForward("/manage/article/form.jsp");
		}
		return new ActionForward("article.do?method=listArticleByPage", true);
	}
	
	public ActionForward getById(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		req.setAttribute("article", articleService.getById(id));
		return new ActionForward("/manage/article/form.jsp");
	}
	
	public ActionForward listArticleByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Article> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", articleService.getByPage(pv));
		return mapping.findForward("list-article");
	}
	
}
